package com.hp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hp.entity.Department;
import com.hp.entity.Employee;
import com.hp.entity.Position;
import com.hp.mapper.DepartmentMapper;
import com.hp.mapper.EmployeeMapper;
import com.hp.mapper.PositionMapper;
import com.hp.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private DepartmentMapper departmentMapper;


    /*根据传入的pageNo 返回page对象*/
    @Override
    public Page<Employee> selectListByPage(int pageNo) {

        Page<Employee> employee_number = employeeMapper.selectPage(new Page<>(pageNo, 3), new QueryWrapper<Employee>().orderByDesc("employee_number"));
        List<Employee> records = employee_number.getRecords();

        for (int i = 0; i < records.size(); i++) {
            //简化下面 重新赋值
            Employee employee = selectEmployee(records.get(i));
            records.set(i, employee);
        }
        employee_number.setRecords(records);
        return employee_number;
    }

    /*查询最大的员工编号*/
    @Override
    public Integer selectMaxNum() {
        return employeeMapper.selectMaxNum();
    }


    /*根据传入的employee对象返回一个完整对象信息*/
    @Override
    public Employee selectEmployee(Employee employee) {
        if (employee.getPosition()==null&&employee.getDepartment()==null){
            Department department = new Department();
            department.setDepartmentNumber(employee.getDepartmentNumber());
            department = departmentMapper.selectOne(new QueryWrapper<>(department));
            Position position = new Position();
            position.setPositionNumber(employee.getPositionNumber());
            position = positionMapper.selectOne(new QueryWrapper<>(position));
            employee.setDepartment(department);
            employee.setPosition(position);
        }
        System.out.println(employee);
        return employee;
    }


}
